
/*
打开单选框
url：选择项页面
inputName：ID存储字段
inputSpan：显示选择项的SPAN 或 显示的input
isNeed：是否必填项 
idCell：选择框返ID回列
nameCell：选择框返回显示列
*/
function getBrowser(url, inputName, inputSpan, isNeed, idCell, nameCell) {
	var isNotNullMsg = "\u6b64\u9879\u4e0d\u80fd\u4e3a\u7a7a";//此项不能为空
	var args = new Array(4);
	args[0] = idCell;
	args[1] = nameCell;
	args[2] = document.all(inputName).value;
	args[3] = getInputSpanValue(inputSpan);
	//alert(url)
	var result = window.showModalDialog(url, args, "dialogWidth:650px;dialogHeight:500px;scroll=yes;");
	//直接关闭窗口
	if (result == null) {
		if (isNeed == 1 && (document.all(inputName).value == null || document.all(inputName).value == "")) {
			fillInputSpan(inputSpan, isNotNullMsg, "red");
		}
		return;
	}
	var _id = result[0];
	var _name = result[1];
	//清除选择框值
	if (_id == "clearValue") {
		document.all(inputName).value = "";
		if (isNeed == 1) {
			fillInputSpan(inputSpan, isNotNullMsg, "red");
		} else {
			fillInputSpan(inputSpan, _name, "");
		}
		return;
	} else {
		document.all(inputName).value = _id;
		//alert(document.all(inputName).value);
		fillInputSpan(inputSpan, _name, "");
	}
}
/**
填充显示的Span或input
*/
function fillInputSpan(inputSpan, _name, color) {
	if (document.all(inputSpan).value == null) {
		document.all(inputSpan).style.color = color;
		document.all(inputSpan).innerHTML = _name;
	} else {
		document.all(inputSpan).style.color = color;
		document.all(inputSpan).value = _name;
	}
}
/**
得到Span或input内容
*/
function getInputSpanValue(inputSpan) {
	if (document.all(inputSpan).value == null) {
		return document.all(inputSpan).innerHTML;
	} else {
		return document.all(inputSpan).value;
	}
}
/*关闭窗口*/
function winClose() {
	if (window.parent != null) {
		window.parent.close();
	} else {
		window.close();
	}
}
/*清除值*/
function winClear() {
	var resultArray = new Array();
	resultArray[0] = "clearValue";
	resultArray[1] = "";
	window.parent.returnValue = resultArray;
	window.close();
}
//以下是对弹出对话框的处理
//
function fireInnerText() {
	var ua = navigator.userAgent;
	var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false;
	if (!ie) {
		HTMLElement.prototype.__defineGetter__("innerText", function () {
			var anyString = "";
			var childS = this.childNodes;
			for (var i = 0; i < childS.length; i++) {
				if (childS[i].nodeType == 1) {
					anyString += childS[i].tagName == "BR" ? "\n" : childS[i].innerText;
				} else {
					if (childS[i].nodeType == 3) {
						anyString += childS[i].nodeValue;
					}
				}
			}
			return anyString;
		});
		HTMLElement.prototype.__defineSetter__("innerText", function (sText) {
			this.textContent = sText;
		});
	}
}
/*
得到单选框选中行的ID值
*/
function getSingleData(input) {
	fireInnerText();
	var parentArgs = window.dialogArguments;
	var _id = input.cells[0].innerText;
	_id = _id.replace(" ", "");
	var _name = input.cells[1].innerText;
	if (parentArgs[0] != null) {
		_id = input.cells[parentArgs[0]].innerText;
	}
	if (parentArgs[1] != null) {
		_name = input.cells[parentArgs[1]].innerText;
	}
	var resultArray = new Array();
	resultArray[0] = _id;
	resultArray[1] = _name;
	window.parent.returnValue = resultArray;
	window.close();
}
/*
得到多选框中选中行的IDS值
*/
function getMultiData(input) {
	var idsselected = "";
	var namesselected = "";
	var destList = document.all("srcList");
	var destlen = destList.options.length;
	if (destlen > 100) {
		alert("\u9009\u62e9\u8d85\u8fc7\u9650\u5236100");//选择超过限制100
		return false;
	}
	for (var i = 0; i < destlen; i++) {
		idsselected += "," + destList.options[i].value;
		namesselected += "," + destList.options[i].text;
	}
	idsselected = idsselected.substring(1, idsselected.length);
	namesselected = namesselected.substring(1, namesselected.length);
	if (idsselected == "") {
		//alert();
		winClear();
	} else {
		var resultArray = new Array();
		resultArray[0] = idsselected;
		resultArray[1] = namesselected;
		window.parent.returnValue = resultArray;
		//alert(idsselected);
		winClose();
	}
}
/*
添加选中项
*/
function addSelectedData(input) {//实现与单选的不一样
	fireInnerText();
	var _id = input.cells[0].innerText;
	var _name = input.cells[1].innerText;
	var parentArgs = window.dialogArguments;
	if (parentArgs[0] != null) {
		_id = input.cells[parentArgs[0]].innerText;
	}
	if (parentArgs[1] != null) {
		_name = input.cells[parentArgs[1]].innerText;
	}
	var newEntry = _id + "~" + _name;
	if (!isExistEntry(_id)) {
		addObjectToSelect(document.all("srcList"), newEntry);
	}
}
/*
判断当前实体是否已被选中
*/
function isExistEntry(entry) {
	var destList = document.all("srcList");
	var destlen = destList.options.length;
	for (var i = 0; i < destlen; i++) {
		var optvalue = destList.options[i].value;
		if (entry.trim() == optvalue) {//为什么if(Trim(entry) == Trim(optvalue))的结果会不一样.????
			return true;
		}
	}
	return false;
}
/*
将对象加到选中框中
*/
function addObjectToSelect(obj, str) {
	fireInnerText();
	if (obj.tagName != "SELECT") {
		return;
	}
	var oOption = document.createElement("OPTION");
	obj.options.add(oOption);
	oOption.value = str.split("~")[0].trim();
	oOption.innerText = str.split("~")[1];
}
/**
全部添加
*/
function addAllToList() {
	fireInnerText();
	var table = document.all("BrowserTable");
	var rowlen = table.rows.length;
	for (var i = 1; i < rowlen; i++) {
		var str = table.rows[i].cells[0].innerText + "~" + table.rows[i].cells[1].innerText;
		if (!isExistEntry(table.rows[i].cells[0].innerText)) {
			addObjectToSelect(document.all("srcList"), str);
		}
	}
}
/**
删除选中选项
*/
function deleteFromList() {
	var destList = document.all("srcList");
	var len = destList.options.length;
	for (var i = (len - 1); i >= 0; i--) {
		if ((destList.options[i] != null) && (destList.options[i].selected == true)) {
			destList.options[i] = null;
		}
	}
}
/**
删除所有选中选项
*/
function deleteAllFromList() {
	var destList = document.all("srcList");
	var len = destList.options.length;
	for (var i = (len - 1); i >= 0; i--) {
		if (destList.options[i] != null) {
			destList.options[i] = null;
		}
	}
}
/**
上移选中项
*/
function upFromList() {
	var destList = document.all("srcList");
	var len = destList.options.length;
	for (var i = 0; i <= (len - 1); i++) {
		if ((destList.options[i] != null) && (destList.options[i].selected == true)) {
			if (i > 0 && destList.options[i - 1] != null) {
				fromtext = destList.options[i - 1].text;
				fromvalue = destList.options[i - 1].value;
				totext = destList.options[i].text;
				tovalue = destList.options[i].value;
				destList.options[i - 1] = new Option(totext, tovalue);
				destList.options[i - 1].selected = true;
				destList.options[i] = new Option(fromtext, fromvalue);
			}
		}
	}
}
/**
下移选中项
*/
function downFromList() {
	var destList = document.all("srcList");
	var len = destList.options.length;
	for (var i = (len - 1); i >= 0; i--) {
		if ((destList.options[i] != null) && (destList.options[i].selected == true)) {
			if (i < (len - 1) && destList.options[i + 1] != null) {
				fromtext = destList.options[i + 1].text;
				fromvalue = destList.options[i + 1].value;
				totext = destList.options[i].text;
				tovalue = destList.options[i].value;
				destList.options[i + 1] = new Option(totext, tovalue);
				destList.options[i + 1].selected = true;
				destList.options[i] = new Option(fromtext, fromvalue);
			}
		}
	}
}

